#include "mygraph.h"

MyGraph::MyGraph() :Graph() {
	name_map = get(vertex_name, *this); //mapping the vertex
	site_map = get(vertex_site, *this);
	edge_map = get(edge_name, *this); //mapping the edges
	port_in = get(edge_portin, *this);
	port_out = get(edge_portout, *this);
}

void MyGraph::addVertex(string* name, int _line) {

	tmp = add_vertex(*this);
	name_map[tmp] = *name;
	name_vertex[*name] = tmp;
	vertex_line[tmp] = _line;
        vert_bm.insert(bm_vertex::value_type(tmp, *name));
}

string MyGraph::getVertexName(Vertex target)
{
    return name_map[target];
}

void MyGraph::setSite(string* site) {
	
	site_map[tmp] = *site; /* site position */
	site_vertex[*site] = tmp; /* retrieve vertex by site */
}

Vertex MyGraph::getVertexBySite(string* site) {

	return site_vertex[*site];
}

void MyGraph::setOutPin(string* vertex, string* out_pin, int _line) {
	
	src = name_vertex[*vertex];
	pin_name_tmp = out_pin;
        out_pin_line = _line;

}

void MyGraph::addInPin(string* vertex, string* in_pin, int _line) {

	dest = name_vertex[*vertex];
	list.push_back( pair< Vertex, string* >(dest, in_pin) );
        list_in_pin_line.push_back(_line);
	
}

void MyGraph::setNet(string* name) {
	
	name->erase(name->begin());
	name->erase( --name->end());
	
	int s = list.size();
	for (int i=0; i<s; i++) {
		std::pair<edge_descriptor, bool> tmp_pair = add_edge(src, list[i].first,
			Edge_Name(*name, 
				Ports(*pin_name_tmp, 
					Porti(*list[i].second) ) ), *this);
                edge_line[tmp_pair.first] = pair<int,int>(out_pin_line, list_in_pin_line[i]);
	}
	list.clear();
        list_in_pin_line.clear();

}
void MyGraph::setNet(string* name, string* type) {

	setNet(name);

}

dynamic_properties* MyGraph::getDProperties() {
	return &dp;
}

void MyGraph::setDProperties() {
	dp.property("id", get(vertex_name, *this));
	dp.property("site", get(vertex_site, *this));
	dp.property("label", get(edge_name, *this));
	dp.property("port_in", get(edge_portin, *this));
	dp.property("port_out", get(edge_portout, *this));
}

void MyGraph::writeGraph(char* file_name) {
	setDProperties();
	filebuf fb;
	fb.open (file_name,ios::out);
	ostream os(&fb);
	write_graphviz(os, *this, dp, string("id"));

}

int MyGraph::getVertexLine(Vertex target)
{
    return vertex_line[target];
}

vector< pair<int,int> >* MyGraph::getNetLines(Vertex vert)
{
    net_list.clear();
    edge_iterator ei, ei_end;
    for(tie(ei, ei_end) = edges(*this); ei != ei_end; ++ei)
    {
        if(source(*ei, *this) == vert || target(*ei, *this) == vert)
        {
            net_list.push_back(edge_line[*ei]);
        }
    }
    return &net_list;
}

bm_vertex* MyGraph::getVertexMap()
{
    
    return &vert_bm;

}

string MyGraph::getVertexSite(Vertex target)
{
    return site_map[target];
}